package com.qiaofang.common.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.qiaofang.common.constant.CommonResponseCode;
import com.qiaofang.common.constant.CommonResponseType;
import com.qiaofang.common.utils.StringUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.Map;

@ApiModel
public class CommonResponse implements Serializable {
    private static final long serialVersionUID = -8349696635548886541L;
    @ApiModelProperty(
            value = "响应码,1表示成功",
            example = "1"
    )
    private String responseCode;
    @ApiModelProperty("响应消息")
    private String responseMessage;
    @ApiModelProperty("响应类型，默认error")
    private String responseType;
    @ApiModelProperty(
            value = "自定义错误数据map",
            example = "${name}${age}${test}"
    )
    @JsonInclude(Include.NON_NULL)
    private Map<String, Object> errorDataMap;

    public Map<String, Object> getErrorDataMap() {
        return this.errorDataMap;
    }

    public void setErrorDataMap(Map<String, Object> errorDataMap) {
        this.errorDataMap = errorDataMap;
    }

    public String getResponseType() {
        if (StringUtil.isBlank(this.responseType) && !CommonResponseCode.RC_SUCCESS.getResponseCode().equals(this.responseCode)) {
            this.responseType = CommonResponseType.ERROR.toString();
        }

        return this.responseType;
    }

    public void setResponseType(String responseType) {
        this.responseType = responseType;
    }

    public String getResponseCode() {
        return this.responseCode;
    }

    public void setResponseCode(String responseCode) {
        this.responseCode = responseCode;
    }

    public CommonResponse() {
        this.responseCode = CommonResponseCode.RC_SUCCESS.getResponseCode();
        this.responseMessage = CommonResponseCode.RC_SUCCESS.getResponseMessage();
    }

    public CommonResponse(String responseCode, String responseMessage) {
        this.responseCode = CommonResponseCode.RC_SUCCESS.getResponseCode();
        this.responseMessage = CommonResponseCode.RC_SUCCESS.getResponseMessage();
        this.responseCode = responseCode;
        this.responseMessage = responseMessage;
    }

    public CommonResponse(CommonResponseCode commonResponseCode) {
        this.responseCode = CommonResponseCode.RC_SUCCESS.getResponseCode();
        this.responseMessage = CommonResponseCode.RC_SUCCESS.getResponseMessage();
        this.responseCode = commonResponseCode.getResponseCode();
        this.responseMessage = commonResponseCode.getResponseMessage();
    }

    public String getResponseMessage() {
        return this.responseMessage;
    }

    public void setResponseMessage(String responseMessage) {
        this.responseMessage = responseMessage;
    }
}
